package com.personnel.interceptor;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: YuePeng
 * @Date: 2021/12/31 17:12
 * @Version: 1.0
 */
@Configuration
public class SourcesWebConfigurer implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //创建自定义拦截器对象
        HandlerInterceptor interceptor=new LoginInterceptor();
        //配置白名单：存放在一个List集合
        List<String> patterns=new ArrayList<>();
        patterns.add("/login");
        patterns.add("/loginByPhone");
        patterns.add("/sendMailCode");
        patterns.add("/sendSMS");
        patterns.add("/checkCode");
        patterns.add("/staff/updatePasswordByUuid");
        patterns.add("/laborContract/selectAll");
        patterns.add("/login.html");
        patterns.add("/loginByPhone.html");
        patterns.add("/page/publicPage/404.html");
        patterns.add("/page/publicPage/findPassword.html");
        patterns.add("/page/publicPage/findPasswordByEmail.html");
        patterns.add("/css/**");
        patterns.add("/js/**");
        patterns.add("/img/**");
        patterns.add("/images/**");
        patterns.add("/upload/**");
        patterns.add("/api/**");
        patterns.add("/lib/**");
        patterns.add("/test/**");
        //完成拦截器的注册
        registry.addInterceptor(interceptor)
                .addPathPatterns("/**")        //表示要拦截url是什么  /**（拦截所有的）
                .excludePathPatterns(patterns);//表示不拦截什么（白名单）
    }

    /** 解决图片上传不能及时显示问题,添加虚拟路径映射
         bug原因:
        这是因为对服务器的保护措施导致的，服务器不能对外部暴露真实的资源路径，需要配置虚拟路径映射访问。
    */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/upload/uploadImg/**")
                //本地测试环境
                .addResourceLocations("file:D:\\IdeaProject\\school\\personnel\\src\\main\\resources\\static\\upload\\uploadImg\\");

                //linux环境
                //.addResourceLocations("file:/home/springboot/upload/uploadImg");
    }
}
